// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database();
const $ = db.command.aggregate;

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();
  let openid = wxContext.OPENID;
  let limit_num = event.limit;
  let skip_num = event.skip;
  console.log('openid:'+openid+';limit_num:'+limit_num+';skip_num:'+skip_num);
  let res = new Object();
  let goods_length = await db.collection('ops_goods').where({
    Issuer_openid: openid,
    status: 1
  }).count();
  let goods_list = await db.collection('ops_goods').aggregate().match({
    Issuer_openid: openid,
    status: 1
  }).project({
    goods_explain: 1,
    goods_name: 1,
    goods_url: 1,
    specs: 1,
    type: 1,
    update_time_show: $.dateToString({
      date: '$update_time',
      format: '%Y-%m-%d %H:%M:%S'
    })
  }).limit(limit_num).skip(skip_num).end();
  res.goods_length = goods_length.total;
  res.goods_list = goods_list.list;
  return res;
}